package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.MyCarInfoEntity;
import com.sgb.kjwl.model.entity.transport.SingleItemEntity;
import com.sgb.kjwl.utils.DefaultUtil;
import com.sgb.kjwl.utils.VehicleConstant;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LogisticsEvent;
import com.sgb.net.util.RequestUtil;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.NumberUtil;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class SelfCarDetailViewModel extends BaseViewModel {

    public MutableLiveData<MyCarInfoEntity> ldEntity = new MutableLiveData<>();
    public MutableLiveData<Boolean> ldCheckOrder = new MutableLiveData<>();

    public SelfCarDetailViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    /**
     * 获取车辆详情
     */
    public void getVehicleDetail(long no) {
        //1自营 2挂靠 个人的车辆详情不传，团队/企业身份后台自行判断
        NewAPIManager.getInstance().getLogisticsAPI().getGroupCarDetail(no, 1)
                .enqueue(new ICallBack<BaseEntity<MyCarInfoEntity>>() {
                    @Override
                    public void onSuccess(BaseEntity<MyCarInfoEntity> data) {
                        if (data.getData() == null) {
                            MToast.showToast(getApplication(), "暂无数据");
                        } else {
                            ldEntity.postValue(data.getData());
                        }
                    }
                });
    }

    /**
     * 同步车辆信息
     */
    public void syncCarInfo(List<SingleItemEntity> list, MyCarInfoEntity entity) {
        if (list == null || entity == null) return;
        list.clear();
        list.add(new SingleItemEntity("车\u3000\u3000型", DefaultUtil.getString(entity.getCarTypeName(), "/")));
        list.add(new SingleItemEntity("车\u3000\u3000长", DefaultUtil.getString(entity.getCarLenName(), "/")));
        list.add(new SingleItemEntity("额定载重", String.format("%s吨", NumberUtil.trim0(entity.getWeight()))));
        list.add(new SingleItemEntity("车牌颜色", getLicensePlateColorWords(entity.getLicensePlateColor())));

        if (!TextUtils.isEmpty(entity.getImageIssuingAuthority())) {
            list.add(new SingleItemEntity("发证机关", entity.getImageIssuingAuthority()));
        }
        if (!TextUtils.isEmpty(entity.getImageVehicleEnergyType())) {
            list.add(new SingleItemEntity("能源类型", entity.getImageVehicleEnergyType()));
        }

        if ((entity.getDetailAddress().contains("省") && entity.getDetailAddress().contains("市"))) {
            list.add(new SingleItemEntity("车辆所在地", DefaultUtil.getString(entity.getDetailAddress())));
        } else {
            if (TextUtils.isEmpty(entity.getProvinceName()) || entity.getDetailAddress().contains(entity.getProvinceName()) && TextUtils.isEmpty(entity.getCityName()) || entity.getDetailAddress().contains(entity.getCityName())) {
                list.add(new SingleItemEntity("车辆所在地", DefaultUtil.getString(entity.getDetailAddress())));
            } else {
                list.add(new SingleItemEntity("车辆所在地", String.format("%s/%s/%s/%s",
                        entity.getProvinceName(),
                        entity.getCityName(),
                        entity.getDistrictName(), DefaultUtil.getString(entity.getDetailAddress()))));
            }
        }

        // list.add(new SingleItemEntity("详细地址", DefaultUtil.getString(entity.getDetailAddress())));
        if (!NumberUtil.isNullOrZero(NumberUtil.convertToDouble(entity.getHeightHurdle()))) {
            list.add(new SingleItemEntity("栏\u3000\u3000高", String.format("%s米", VehicleConstant.getTrim0Str(entity.getHeightHurdle()))));
        }
        if (entity.getIsDump() != null) {
            list.add(new SingleItemEntity("是否自卸", DefaultUtil.getString(VehicleConstant.getIsDumpStr(entity.getIsDump()), "/")));
        }
        if (!TextUtils.isEmpty(entity.getFunctionNos())) {
            list.add(new SingleItemEntity("车辆功能", DefaultUtil.getString(entity.getFunctionNos(), "/")));
        }
        if (!TextUtils.isEmpty(entity.getCarProvinceName())) {
            list.add(new SingleItemEntity("车辆上户地", String.format("%s/%s/%s",
                    entity.getCarProvinceName(),
                    entity.getCarCityName(),
                    entity.getCarDistrictName())));
        }
        if (!NumberUtil.isNullOrZero(entity.getInsuranceTime())) {
            list.add(new SingleItemEntity("保险时间", !NumberUtil.isNullOrZero(entity.getInsuranceTime())
                    ? DateUtils.timestampToDate(entity.getInsuranceTime())
                    : "/"));
        }
        if (!NumberUtil.isNullOrZero(entity.getAnnualInspectionTime())) {
            list.add(new SingleItemEntity("年检时间", !NumberUtil.isNullOrZero(entity.getAnnualInspectionTime())
                    ? DateUtils.timestampToDate(entity.getAnnualInspectionTime())
                    : "/"));
        }
        if (!NumberUtil.isNullOrZero(entity.getUpMaintainTime())) {
            list.add(new SingleItemEntity("上次保养时间", !NumberUtil.isNullOrZero(entity.getUpMaintainTime())
                    ? DateUtils.timestampToDate(entity.getUpMaintainTime())
                    : "/"));
        }
        if (!NumberUtil.isNullOrZero(entity.getCarYears())) {
            list.add(new SingleItemEntity("车辆年限", !NumberUtil.isNullOrZero(entity.getCarYears())
                    ? String.format("%s年", entity.getCarYears())
                    : "/"));
        }
        if (!TextUtils.isEmpty(entity.getIntentionProvinceName())) {
            list.add(new SingleItemEntity("当前去向", String.format("%s %s",
                    entity.getIntentionProvinceName(),
                    entity.getIntentionCityName())));
        }
    }

    /**
     * 获取车辆颜色
     */
    public String getLicensePlateColorWords(String licensePlateColor) {
        if (TextUtils.isEmpty(licensePlateColor)) {
            licensePlateColor = "/";
        } else if ("1".equals(licensePlateColor)) {
            licensePlateColor = "蓝色";
        } else if ("2".equals(licensePlateColor)) {
            licensePlateColor = "黄色";
        } else if ("9".equals(licensePlateColor)) {
            licensePlateColor = "绿色";
        }
        return licensePlateColor;
    }

    /**
     * 检查是否有绑定订单
     */
    public void checkOrder(Context context, String driverNo, String carNo) {
        RequestUtil requestUtil = RequestUtil.map();
        requestUtil.add("no", driverNo);
        requestUtil.add("carNo", carNo);
        requestUtil.add("isLooseDriver", 2);
        DialogHelper.showProgressDialog(context, "加载中");
        NewAPIManager.getInstance().getLogisticsAPI().checkIsHaveOrders(requestUtil.buildMap())
                .enqueue(new ICallBack<BaseEntity<Boolean>>() {
                    @Override
                    public void onSuccess(BaseEntity<Boolean> data) {
                        ldCheckOrder.postValue(data.getData());
                    }
                });
    }

    /**
     * 解绑司机
     */
    public void unbindDriver(Context context, String no) {
        DialogHelper.showProgressDialog(context, "加载中");
        NewAPIManager.getInstance().getLogisticsAPI().unbindDriver(no)
                .enqueue(new ICallBack<BaseEntity>() {
                    @Override
                    public void onSuccess(BaseEntity data) {
                        EventBus.getDefault().post(LogisticsEvent.make().setMessageTag(EventConstant.EVENT_UNBIND_DRIVER));
                    }
                });
    }
}
